<template>
    <div>
        <!-- Breadcrumb 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/registlevel' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>挂号等级管理</el-breadcrumb-item>
      <el-breadcrumb-item>挂号等级列表</el-breadcrumb-item>
    </el-breadcrumb>

     <!-- 搜索 -->
    <el-row :gutter="20">
      <el-col :span="12">
        <el-input placeholder="挂号等级查询" >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getRegList"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="depDialogVisible = true"
          >新增挂号等级</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-button type="success" @click="editDialogVisible = true"
          >查询挂号等级</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-button type="danger" @click="removeBatch(sels)">批量删除</el-button>
      </el-col>
    </el-row>

     <!-- card展示 -->
    <el-card>
 <!-- 数据表格 -->
      <el-table  :data="regList" border stripe>
        <!-- 为表格添加一个编号（索引） -->
          <el-table-column  type="selection" width="65" ></el-table-column>
        <el-table-column type="index" label="编号"> </el-table-column>
        <el-table-column  prop="registcode" label="号别编码"></el-table-column>
        <el-table-column  prop="registname" label="号别名称"> </el-table-column>
        <el-table-column prop="sequenceno" label="显示顺序号" > </el-table-column>
        <el-table-column prop="registfee"  label="挂号费" > </el-table-column>
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini" 
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeUserById(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
        <!-- Pagination分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

     <!-- 查询用户的对话框 -->
    <el-dialog
      title="查询用户"
      :visible.sync="editDialogVisible"
      width="80%"
    >
      <el-form
        :model="qyertForm"
        label-width="100px"
      >
        <el-form-item label="号别编码">
          <el-input v-model="qyertForm.registcode" prop="registcode" ></el-input>
        </el-form-item>
        <el-form-item label="号别名称" prop="registname">
          <el-input v-model="qyertForm.registname"></el-input>
        </el-form-item>
        <el-form-item label="显示顺序号" prop="sequenceno">
          <el-input v-model="qyertForm.sequenceno"></el-input>
        </el-form-item>
        <!-- <el-form-item label="用户类别" prop="useType">
          <el-input v-model="qyertForm.useType"></el-input>
        </el-form-item>
          <el-form-item label="医生职称" prop="docTitleId">
          <el-input v-model="qyertForm.docTitleId"></el-input>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click=" editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="queryUser">查询</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加挂号等级" :visible.sync="depDialogVisible" width="50%">
      <!-- 创建一个表单 -->
      <!--添加挂号等级  -->
      <!-- 写完表单后要添加数据绑定以及检验的规则 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef">
        <el-form-item prop="registcode" label="号别编码 ">
          <el-input
            v-model="addForm.registcode"
            prefix-icon="iconfont icon-user"
            placeholder="请填写号别编码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="registname" label="号别名称 ">
          <el-input
            v-model="addForm.registname"
            prefix-icon="iconfont icon-user"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="sequenceno" label="显示顺序号 ">
          <el-input
            v-model="addForm.sequenceno"
            prefix-icon="iconfont icon-user"
            placeholder="请输显示顺序号 "
          ></el-input>
        </el-form-item>
        <el-form-item prop="registfee" label="挂号费">
          <el-input
            v-model="addForm.registfee"
            prefix-icon="iconfont icon-user"
            placeholder="请输挂号费"
          ></el-input>
        </el-form-item>
      
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </span>
    </el-dialog>


    <el-dialog title="修改用户" :visible.sync="updateDialogVisible" width="50%">
      <!-- 创建一个表单 -->
      <!-- 修改 -->
      <!-- 写完表单后要修改数据绑定以及检验的规则 -->
      <el-form :model="updateForm" :rules="updateFormRules" ref="updateFormRef">
        <el-form-item prop="registcode" label="号别编码">
          <el-input
            v-model="updateForm.registcode"
            prefix-icon="iconfont icon-user"
            placeholder="请填写账号信息"
          ></el-input>
        </el-form-item>
        <el-form-item prop="registname" label="号别名称">
          <el-input
            v-model="updateForm.registname"
            prefix-icon="iconfont icon-user"
            placeholder="请输入号别名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="sequenceno" label="顺序号">
          <el-input
            v-model="updateForm.sequenceno"
            prefix-icon="iconfont icon-user"
            placeholder="请输显示顺序号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="registfee" label="挂号费">
          <el-input
            v-model="updateForm.registfee"
            prefix-icon="iconfont icon-user"
            placeholder="请输显示挂号费"
          >{{updateForm.realName}}</el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="sureUpdate">确 定</el-button>
      </span>
    </el-dialog>

    </div>
</template>

<script>
export default {
     data(){
      return{
       queryInfo:{
          pageNum:1,
          pageSize:2
        },
        //根据条件查询提交的条件
        qyertForm:{
            registcode:'',
          registname:'',
          sequenceno:null,
          registfee:null,
            id:null,
            pageNum:1,
            pageSize:2
        },
        //添加用户的表单数据addForm
        addForm:{
          registcode:'',
          registname:'',
          sequenceno:null,
          registfee:null,
          
        },
        //修改用户的表单数据updateForm
         updateForm:{
           id:null,
           registcode:'',
          registname:'',
          sequenceno:null,
          registfee:null,
        },
        //添加用户表单验证 addFormRules
        total:0,
         //保存用户的数据
      regList: [],
        //替换数据  用户所在科数组
      deptNameList:[],
      //添加Dilog框默认关闭
      depDialogVisible:false,
      //查询Dilog框默认关闭
      editDialogVisible:false,
      //修改Dilog框默认关闭
      updateDialogVisible:false,
      //添加信息表单验证
      addFormRules:{
        registcode:[
       { required: true, message: "请输入", trigger: "blur" },
        ],
       registname: [
          { required: true, message: "请输入", trigger: "blur" },
      
        ],
        sequenceno:[
            { required: true, message: "请输顺序号", trigger: "blur" },
         
         ],
         registfee:[  { required: true, message: "费用", trigger: "blur" }]
        
      },
      //修改信息表单验证
      updateFormRules:{
         registcode:[
       { required: true, message: "请输入", trigger: "blur" },
        ],
       registname: [
          { required: true, message: "请输入", trigger: "blur" },
      
        ],
        sequenceno:[
            { required: true, message: "请输顺序号", trigger: "blur" },
         
         ],
         registfee:[  { required: true, message: "费用", trigger: "blur" }]
        //  deptId:[  { required: true, message: "请输真实姓名", trigger: "blur" }]
        
      },
      //复选框id数组
      sels:[],
      }
      },
      created() {
    this.getRegList();
    // this.getDeptNameList();
  },
    methods:{
      //查询所有数据
     async getRegList(){
         //向后台发送请求获取数据
      const { data: res } = await this.$http.get("registlevel/queryAll",
      { params: this.queryInfo}
      );
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户信息失败");
      }
      // 如果状态正常，获取用户数据
      this.regList = res.list;
      this.total = res.total;
      console.log(res);
    },
    //获取DeptNameList数组的方法
    // async getDeptNameList(){
    //  const {data:res}=await this.$http.get(`department/queryDeptName`, 
    //   { params: this.queryInfo});
    //   // 如果状态异常
    //   if (res.meta.status !== 200) {
    //     return this.$message.error("获取医生职称信息失败");
    //   }
    // this.deptNameList=res.list;
    // console.log(res);
    // },
     handleSizeChange(size) {
      // console.log(size)
      this.queryInfo.pageSize = size;
      //重新查询用户信息
      this.getRegList();
    },
     handleCurrentChange(page){
      // console.log(page)
      this.queryInfo.pageNum = page;
      //重新查询用户信息
    this.getRegList();
    },
     //根据条件查询用户信息
    async queryUser(){
     const { data: res } = await this.$http.post("registlevel/queryReg",this.qyertForm);
     //如果状态异常
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户信息失败");
      }
       // 如果状态正常，获取用户数据
        this.regList = res.registlevels;
      this.total = res.total;
      console.log(res);
       this.editDialogVisible=false
    },
    
    //替换数据 用户类型
    usetypeFormat(row){
      let index = row.usetype;
     
      return this.userTypeName[index-1];
    },
     //替换数据 用户所在科室
    deptidFormat(row){
       let deptName;
       this.deptNameList.forEach(value=>{
      if(row.deptid==value.id){
        deptName=value.deptname
    }
       })
      return deptName
     },
     //替换数据 医生职称
     docTitleFormat(row){
       let index=row.doctitleid
        if(index==1){
        index=80
      }
       return this.docTitleList[index-80]
     },


    //删除用户信息
    async removeUserById(row){
     const{data:res}=await this.$http.get(`user/deleteUserById`, {
        params: {id:row}
      })
       // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
       this.getUsersList();
    },

    //添加信息 取消
    cancelAdd(){           
      this.$refs.addFormRef.resetFields();
      this.depDialogVisible = false;
    },

    //添加信息 确定
   async sureAdd(){
     const {data:res}= await this.$http.post(`registlevel/insertReg`, 
       this.addForm
      )
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
    //  this.getDeptNameList();
    this.getRegList();
     this.$refs.addFormRef.resetFields();
        this.depDialogVisible = false;
        
    },
    //批量删除 复选框 选中id放进数组
    selsChange(sels){
     this.sels=sels
    } ,

    //批量删除 向后台发送请求
  async removeBatch(rows){
  // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)

      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

    var ids = [];
    rows.forEach(element=>{
      ids.push(element.id)
    })

    const{data:res} =await this.$http.post(`registlevel/deleteByChoose`,{info:ids});
     // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
      this.getRegList();
 },

    //打开修改用户的Dilog窗口
   async showEditDialog(row){
      this.updateDialogVisible=true;  
      //根据id获取用户信息
      const{data:res}=await this.$http.get(`registlevel/selectUserById`,{  params: {id:row}})
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("根据id用户信息失败");}
        console.log(res)
             this.updateForm.registcode=res.registlevel.registcode
             this.updateForm.registname=res.registlevel.registname
             this.updateForm.sequenceno=res.registlevel.sequenceno
             this.updateForm.registfee=res.registlevel.registfee
              this.updateForm.id=row
       },
       //发送 修改用户  请求
    async sureUpdate(){
       const{data:res}=await this.$http.post(`registlevel/updateReg`,this.updateForm);
       if (res.meta.status !== 200) {
        return this.$message.error("修改用户信息失败");
      }
    //    this.getDeptNameList();
    this.$refs.updateFormRef.resetFields();
    this.updateDialogVisible=false;
     this.getRegList();
    }
    }
}
</script>

<style lang="less" scoped>

</style>